<template>
  <div>
      <page-header :title="'宣讲会 ' + title" :content="'seminar ' + type" />
      <page-main>
          <el-row>
              <el-col :span="24">
                  <el-form ref='form' :model="form" label-width="120px">
                      <el-form-item label="企业名称" label-width="120px">   
                        <el-col :span="12">
                            <el-input v-model="form.companyname" :disabled="type == 'view'"/>
                        </el-col>
                      </el-form-item>
                      <el-form-item label="企业类型" label-width="120px">   
                        <el-col :span="6">
                            <el-select
                                v-model="form.companytype"
                                filterable
                                allow-create
                                default-first-option
                                :disabled="type == 'view'"
                                placeholder="请选择企业">
                                <el-option
                                v-for="item in options"
                                :key="item.value+item.label"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                      </el-form-item>
                      <el-form-item label="宣讲会地址" label-width="120px">
                        <el-col :span="12">
                            <el-input v-model="form.address" :disabled="type == 'view'"/>
                        </el-col>
                      </el-form-item>
                      <el-form-item label="宣讲会日期">
                          <el-date-picker
                            v-model="form.seminardate"
                            :disabled="type == 'view'"
                            align="right"
                            type="date"
                            placeholder="选择日期"
                            value-format="yyyy-MM-dd"
                            :picker-options="pickerOptions">
                          </el-date-picker>
                      </el-form-item>
                      <el-form-item label="宣讲会时间" label-width="120px">
                          <el-time-picker
                            is-range
                            v-model="form.seminartime"
                            :disabled="type == 'view'"
                            value-format="HH:mm:ss"
                            range-separator=","
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            placeholder="选择时间范围">
                          </el-time-picker>
                      </el-form-item>
                      <el-form-item label="小提示" label-width="120px">
                          <el-input
                            :disabled="type == 'view'"
                            type="textarea"
                            placeholder="请输入内容"
                            v-model="form.tip"
                            maxlength="100"
                            show-word-limit
                            >
                          </el-input>
                      </el-form-item>
                      <el-form-item label="">
                        <el-button type="primary" v-if="type != 'view'" @click="update()">确定</el-button>
                        <el-button @click="cannelBuild()">取消</el-button>
                      </el-form-item>
                  </el-form>
              </el-col>
          </el-row>
      </page-main>
  </div>
</template>

<script>
export default {
    name: 'recruitment_update',
    data() {
        return {
            type: '',
            title: '',
            id: '',
            form: {},
            options: [{
                value: '民营企业',
                label: '民营企业'
            },{
                value: '国有企业',
                label: '国有企业'
            }],
            pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                    picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24);
                    picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', date);
                    }
                }]
            },
        }
    },
    watch:{
        '$route.path': function(newVal,oldVal){
            //console.log(newVal+"---"+oldVal);
            console.log(newVal);
        }
    },
    beforeRouteEnter(to, from, next) {
        let that = this
        next(vm => {
            // 通过 `vm` 访问fetchData，将query参数name，传递过去，进行逻辑处理
            // that.type = vm.$route.params.type
            vm.fetchData(vm.$route.params.type, vm.$route.params.id);
        });
    },
    methods: {
        fetchData(type, id) {
            this.type = type
            let titleList = {
                add: '新增',
                edit: '编辑',
                view: '查看'
            }
            this.id = id
            this.title = titleList[type]
            if (id != '*') {
                this.$util.post('public/selectSeminarsByPrimaryKey', {id: id}).then(res => {
                    this.form = res
                })
            }
        },
        cannelBuild() {
            this.form = {}
            this.$router.back(-1)
        },
        update() {
            if (this.type == 'add') {
                this.$util.post('public/insertSeminars', this.form).then(res => {
                    this.$message({
                        message: '新增成功',
                        type: 'success'
                    });
                    this.$router.push('/employment_manager/seminarList')
                })
            } else {
                this.$util.post('public/updateSeminarsByPrimaryKey', this.form).then(res => {
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                    this.$router.push('/employment_manager/seminarList')
                })
            }
        }
    }
}
</script>
